const db = wx.cloud.database()
const _ = db.command
var app = getApp()
Page({ 

    /**
     * 页面的初始数据
     */
    data: {
		team:{},
		aboutMatchs:[], 
		applyPeople:[],
		applyPeopleId:[], 
		status:"已结束",
		ismyself:false
    },
	apply(){
		if (!this.data.applyPeopleId.includes(app.globalData.openid)){
			wx.cloud.callFunction({
				name:"apply",
				data:{_id:this.data.team._id}
			}).then(res=>{
				let applyPeopleId = this.data.applyPeopleId
				applyPeopleId.push(app.globalData.openid)
				this.setData({
					applyPeopleId:applyPeopleId
				})
				wx.showToast({
					title: '报名成功',
				})
			})
		}else{
			wx.showToast({
				title: '已经报名过了',
				icon:"none"
			})
		} 
	},
	admin(){
		wx.navigateTo({
			url: '/pages/teamAdmin/teamAdmin?teamid='+this.data.team.teamId,
		})
	},
    member(e) {
        wx.navigateTo({ 
			url: '/pages/member/member?teamPeopleId=' + this.data.team.teamPeople + "&id=" + this.data.team._id,
            success: (result) => {

            },
            fail: () => {},
            complete: () => {}
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
		console.log(options.teamId)
		db.collection("team").where({ teamId: options.teamId}).get()
		.then(res=>{
			if (res.data[0].applyPeople == null){
				res.data[0].applyPeople = []
			}
			this.setData({
				team:res.data[0],
				applyPeopleId: res.data[0].applyPeople
			})
			if (app.globalData.openid == this.data.team._openid) {
				this.setData({
					ismyself: true
				})
			}
			if(this.data.team.status){
				this.setData({
					status:"进行中"
				})
			}
			db.collection("match").where({
				matchId: _.in(this.data.team.aboutMatchId)
			}).get().then(eres => {
				console.log(eres)
				this.setData({
					aboutMatchs: eres.data
				})
			})
			db.collection("users").where({
				_openid: _.in(this.data.team.applyPeople)
			}).get().then(eres => {
				console.log(eres)
				this.setData({
					applyPeople: eres.data
				})
			})
		})
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
		
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function(e) {
	
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})